package com.net.lnk.rpc.http.container.http;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.AbstractHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;

import com.net.lnk.rpc.http.container.RpcContainer;
import com.net.lnk.rpc.http.invoke.ProviderConfig;

/**
 * HTTP请求监听器，主要设置监听端口和请求处理，使用Jetty
 */
public class HttpContainer extends RpcContainer {

	private AbstractHandler httpHandler;
	private ProviderConfig providerConfig;

	public HttpContainer(AbstractHandler httpHandler) {
		this(httpHandler, new ProviderConfig());
	}

	public HttpContainer(AbstractHandler httpHandler, ProviderConfig providerConfig) {
		this.httpHandler = httpHandler;
		this.providerConfig = providerConfig;
		RpcContainer.container = this;
	}

	public void start() {
		Server server = new Server();
		try {
			SelectChannelConnector connector = new SelectChannelConnector();
			connector.setPort(providerConfig.getPort());
			server.setConnectors(new Connector[] { connector });
			server.setHandler(httpHandler);
			server.start();
		} catch (Throwable e) {
			throw new RuntimeException("Start http container failed!");
		}
	}

}
